package net.hvacapp.cmvrfforandroid.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.hvacapp.cmvrfforandroid.R;


/**
 * Custom Action Bar providing methods for adding one left and one right Action
 * as well as a center View.
 * <p/>
 * This widget is intended to be used as the top bar. Not setting right, left or
 * center view will leave the ActionBar empty.
 * <p/>
 * To set the center view, use the {@link #setCenterView(android.view.View view)
 * setCenterView} method and provide a arbitrary view. This view will be
 * centered in the action bar and match parent height, by default.
 *
 * @author Viktor Nyblom
 */
public class CMActionBar extends RelativeLayout implements OnClickListener {

    private ViewGroup mLeft;
    private ViewGroup mRight;
    private ViewGroup mCenter;
    private Context mContext;
    private TextView mLeftTextView;
    private TextView mRightTextView;
    private TextView mTitleView;
    private ImageView mLogoView;
    //    private View mHeaderView;
    private TextView mRightSubView;

    /**
     * Interface providing methods for a standard Action.
     */
    public interface Action {
        public int getDrawable();

        public void performAction();
    }

    public CMActionBar(Context context, View view) {
        super(context);
    }

    public CMActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CMActionBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
    }

    public void onCreate(Context context) {
        mContext = context;
        mCenter = (ViewGroup) findViewById(R.id.actionbar_center_stage);
        mLeft = (ViewGroup) findViewById(R.id.actionbar_left_layout);
        mRight = (ViewGroup) findViewById(R.id.actionbar_right_layout);
        mTitleView = (TextView) findViewById(R.id.actionbar_title);
        mLogoView = (ImageView) findViewById(R.id.actionbar_logo);
        mLeftTextView = (TextView) findViewById(R.id.actionbar_left_tv);
        mRightTextView = (TextView) findViewById(R.id.actionBar_right_tv);
        mRightSubView = (TextView) findViewById(R.id.actionbar_right_sub_tv);
    }

    /**
     * Set the Action for the left action button.
     *
     * @param action
     */
    public void setLeftAction(Action action) {
        getLeftView(action);
    }

    public void setRightAction(Action action) {
        getRightView(action);
    }

    public void setRightTextAction(AbstractTextAction action) {
        mRightTextView.setText(action.getTitle());
        mRightTextView.setOnClickListener(this);
        mRightTextView.setTag(action);
    }


    private TextView getLeftView(Action action) {
        mLeftTextView.setBackgroundResource(action.getDrawable());
        mLeftTextView.setOnClickListener(this);
        mLeftTextView.setTag(action);
        return mLeftTextView;
    }

    private TextView getRightView(Action action) {
        mRightTextView.setBackgroundResource(action.getDrawable());
        mRightTextView.setOnClickListener(this);
        mRightTextView.setTag(action);
        return mRightTextView;
    }


    public void setLeftView(View view) {
        setView(mLeft, view);
    }

    public ViewGroup getLeftView() {
        return mLeft;
    }

    private void setView(ViewGroup group, View view) {
        group.removeAllViews();
        if (view != null) {
            group.addView(view, MarginLayoutParams.MATCH_PARENT,
                    MarginLayoutParams.MATCH_PARENT);
        }
    }

    /**
     * Set the view to be displayed in the middle of the action bar. This view
     * will be centered by default.
     *
     * @param view
     */
    public void setCenterView(View view) {
        setView(mCenter, view);
    }

    public void setTitleResource(int resId) {
        setTitle(mContext.getString(resId));
    }

    public void setTitle(String title) {
        setTitle(title, Gravity.CENTER);
    }

    /**
     * 设置AcitonBar的中间标题
     *
     * @param title
     * @param gravity
     */
    public void setTitle(String title, int gravity) {

        if (!isHasTextView(mCenter)) {
            mCenter.removeAllViews();
            mCenter.addView(mTitleView);
        }


        mTitleView.setVisibility(View.VISIBLE);
        mLogoView.setVisibility(View.INVISIBLE);
        mTitleView.setText(title);
        mTitleView.setGravity(gravity);
    }

    public boolean isHasTextView(ViewGroup group) {
        boolean result = false;
        for (int i = 0; i < group.getChildCount(); i++) {
            View view = group.getChildAt(i);
            if (view instanceof TextView) {
                result = true;
                break;
            }
        }
        return result;
    }

    public void setRightTitle(String title) {
        mRightSubView.setText(title);
    }

    @Override
    public void onClick(View v) {
        final Object tag = v.getTag();
        if (tag instanceof Action) {
            final Action action = (Action) tag;
            action.performAction();
        }
    }

    public static abstract class AbstractAction implements Action {
        final private int mDrawable;

        public AbstractAction(int drawable) {
            mDrawable = drawable;
        }

        @Override
        public int getDrawable() {
            return mDrawable;
        }
    }

    public static abstract class AbstractTextAction implements Action {
        public AbstractTextAction() {
        }

        public int getDrawable() {
            return 0;
        }

        public String getTitle() {
            return "";
        }
    }

    public interface OnActionListener {
        public void onAction();
    }

    public void clearRightAction() {
        mRightSubView.setText("");
        setRightAction(new Action() {
            @Override
            public int getDrawable() {
                return 0;
            }

            @Override
            public void performAction() {
            }
        });
    }

    public void clearRightTextView() {
        mRightTextView.setText("");
    }

    public void clearLeftTextView() {
        mLeftTextView.setText("");
    }

    public void clearLeftAction() {
        setLeftAction(new Action() {
            @Override
            public int getDrawable() {
                return 0;
            }

            @Override
            public void performAction() {
            }
        });
    }

    public void clearTitleView() {
        mTitleView.setVisibility(View.INVISIBLE);
        mLogoView.setVisibility(View.VISIBLE);
    }

    public void resetActionBar() {
        clearRightAction();
        clearLeftAction();
        clearTitleView();
    }

}
